<template>
  <div class="app-container">
    
<br>
<br>
    <el-button type="primary" @click="add">添加科室-就诊模板</el-button>
    <br>
    <br>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    > 
      <el-table-column align="center" label="序号" width="50">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="科室名称" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.jobOfficeName }}
        </template>
      </el-table-column>
      <el-table-column label="模板内容" width="250" align="center">
        <template slot-scope="scope">
          <el-tag type="" v-for="(item, index) in scope.row.visitTemplateList" :key="index" style="margin: 0 10px">
          {{ item.templateName }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="操作" align="center" width="300" >
        <template slot-scope="scope">
          <el-button type="primary"  @click="getInfo(scope.row)" size="small">详情</el-button>
          <el-button type="success"  @click="update(scope.row)" size="small">修改</el-button>
          <!-- <el-button type="danger" @click="deleteInfo(scope)" size="small">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="->, total, prev, pager, next"
      :total="this.total"
      :current-page="currentPage"
      :page-size="pageSize"
      @current-change="clickCurPage"
      >
    </el-pagination>
  <el-dialog title="科室-诊断模板详情" :visible.sync="infoVisible">
      <h2>科室-诊断模板</h2>
      <el-form ref="info" :model="info" label-width="120px">
        <el-form-item label="科室">
            <el-select style="width: 100%;" v-model="info.jobOfficeId" placeholder="请选择" :disabled="true">
              <el-option
                v-for="item in jobOfficeEnum"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="模板">
            <el-select style="width: 100%;" v-model="info.visitTemplateCodeList" multiple placeholder="请选择" :disabled="true">
              <el-option
                v-for="item in templateEnum"
                :key="item.templateCode"
                :label="item.templateName"
                :value="item.templateCode">
              </el-option>
            </el-select>
        </el-form-item>
      </el-form>
  </el-dialog>
  <el-dialog :title="dialogTitle" :visible.sync="formVisible" :closeOnClickModal="false">
      <h2>科室-诊断模板</h2>
      <el-form ref="form" :model="form" label-width="120px" :rules="formRules">
       <el-form-item label="科室" prop="jobOfficeId">
            <el-select style="width: 100%;" v-model="form.jobOfficeId" placeholder="请选择" >
              <el-option
                v-for="item in jobOfficeEnum"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="模板" prop="template">
            <el-select style="width: 100%;" v-model="form.visitTemplateCodeList" multiple placeholder="请选择" >
              <el-option
                v-for="item in templateEnum"
                :key="item.templateCode"
                :label="item.templateName"
                :value="item.templateCode">
              </el-option>
            </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="confirm">确 定</el-button>
          <div style="width:200px;display: inline-block;"> </div>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
  </el-dialog>
  </div>
</template>

<script>

import { getList, add, edit, del } from '@/api/visitTemplateJobOffice'
import { templateList } from '@/api/visitTemplate'
import { getList as getJobOfficeList } from '@/api/jobOffice'

export default {
  name: "VisitTemplateJobOffice",
  filters: {

  },
  data() {
    const validateJobOfficeId = (rule, value, callback) => {
      if (!value) callback(new Error('请选择科室')) 
      else callback()
    }
    const validateTemplate = (rule, value, callback) => {
      if (!this.form.visitTemplateCodeList || this.form.visitTemplateCodeList.length === 0) callback(new Error('请选择模板')) 
      else callback()
    }
    return {
      list: null,
      listLoading: true,
      currentPage: 1,
      pageSize: 10,
      total: 0,
      curIndex: -1,
      infoVisible: false,
      formVisible: false,
      info: {},
      form: {},
      dialogTitle: '添加科室模板',
      depts:[],
      formRules: {
        jobOfficeId: [{ required: true, trigger: 'blur', validator: validateJobOfficeId }],
        template: [{ required: true, trigger: 'blur', validator: validateTemplate }],
      },
      jobOfficeEnum: [],
      templateEnum: [],
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getList().then(res=>{
        console.log(res)
        this.list = res.data
        this.listLoading = false
      }).catch(err=>{
        console.log("err")
        console.log(err)
        this.listLoading = false
      })
      getJobOfficeList().then(res=> {
        this.jobOfficeEnum = res.data
      })
      templateList().then(res=> {
        this.templateEnum = res.data
      })
      
    },
    clickCurPage(val){
      this.currentPage = val
      this.fetchData()
    },
    getInfo(row) {
      this.info = row
      this.$set(this.info, 'visitTemplateCodeList', this.info.visitTemplateList.map(item=> item.templateCode))
      this.infoVisible = true
    },
    update(row) {
      this.reset()
      this.dialogTitle = '修改'
      this.form = JSON.parse(JSON.stringify(row))
      this.form.isupdate = true
      this.$set(this.form, 'visitTemplateCodeList', this.form.visitTemplateList.map(item=> item.templateCode))
      this.formVisible = true
    },
    add() {
      this.reset()
      this.dialogTitle = '新增'
      this.formVisible = true
      // this.$router.push({name: 'VisitTemplate', params: row})
    },
    confirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if(this.form.isupdate == true) {  // update
            console.log(this.form)
            edit(this.form).then(res=>{
              console.log(res)
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.formVisible = false
              this.listLoading = false
              this.$tab.refreshPage({path: '/guidance/visitTemplateJobOffice', name: "VisitTemplateJobOffice"})
            }).catch(err=>{
              console.log(err)
            })
          } else { // add
            console.log(this.form)
            add(this.form).then(res=>{
              this.formVisible = false
              this.listLoading = false
              this.$tab.refreshPage({path: '/guidance/visitTemplateJobOffice', name: "VisitTemplateJobOffice"})
            }).catch(err=>{
              console.log(err)
            })
          }
        }
      })
      
    },
    cancel() {
      this.formVisible = false
    },
    reset(){
      this.form = {}
    }
  }
}
</script>
